package homework;

import java.util.Stack;

public class HomeWork28 {
    public boolean backspaceCompare(String s, String t) {
        Stack<Character>stack1=new Stack<>();
        Stack<Character>stack2=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)=='#'){
                if(!stack1.empty()){
                    stack1.pop();
                }
                continue;
            }
            stack1.add(s.charAt(i));
        }
        for (int i = 0; i < t.length(); i++) {
            if(t.charAt(i)=='#'){
                if(!stack2.empty()){
                    stack2.pop();
                }
                continue;
            }
            stack2.add(t.charAt(i));
        }
        while (!stack1.empty()&&!stack2.empty() ){
            if(stack1.pop()!=stack2.pop()){
                return false;
            }
        }
        if(stack1.empty()&&stack2.empty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        HomeWork28 homeWork28=new HomeWork28();
        String s1="ab##";
        String s2="c#d#";
        homeWork28.backspaceCompare(s1,s2);
    }
}
